"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimerManager = void 0;
const RoomModel_1 = require("../../hall_server/model/RoomModel");
const Config_1 = require("../config/Config");
const RoomConfig_1 = require("../config/RoomConfig");
const ServerConfig_1 = require("../config/ServerConfig");
const SocketConnectionManager_1 = require("../engine/socketserver/socket/SocketConnectionManager");
const Logger_1 = require("../engine/utils/Logger");
const RoomManager_1 = require("./RoomManager");
const check_delete_room_time_inteval = 1000; //1秒检查一次
const Must_delete_room_time = 1000 * 60 * 60 * 24; //120分钟强制解散
const check_leave_room_time_inteval = 1000; //1秒检查一次
const Must_leave_room_time = 1000 * 30; //30秒不准备离开
class TimerManager {
    constructor() {
        this.last_check_delete_room_time = 0;
        this.last_check_leave_room_time = 0;
    }
    checkTimer(now) {
        this.checkPlayerOnline(now);
        this.checkDeleteRoom(now);
        this.checkLeaveRoom(now);
    }
    checkPlayerOnline(now) {
        return __awaiter(this, void 0, void 0, function* () {
            if (Config_1.Config.serverType == Config_1.Config.ServerType_List.hall_server) {
                return;
            }
            RoomModel_1.RoomModel.roomMap.forEach((room, key) => {
                let needNotify = false;
                for (let i = 0; i < room.playerList.length; i++) {
                    let player = room.playerList[i];
                    if (player) {
                        if (SocketConnectionManager_1.SocketConnectionManager.instance.isUserOnline(player.userid)) {
                            if (!player.isOnline) {
                                player.isOnline = true;
                                needNotify = true;
                            }
                        }
                        else {
                            if (player.isOnline) {
                                player.isOnline = false;
                                needNotify = true;
                            }
                        }
                    }
                }
                if (needNotify) {
                    RoomManager_1.RoomManager.instance.notify_player_update(room);
                }
            });
        });
    }
    checkLeaveRoom(now) {
        return __awaiter(this, void 0, void 0, function* () {
            if (Config_1.Config.serverType == Config_1.Config.ServerType_List.hall_server) {
                return;
            }
            if (now - this.last_check_leave_room_time >= check_leave_room_time_inteval) {
                this.last_check_leave_room_time = now;
                RoomModel_1.RoomModel.roomMap.forEach((value, key) => {
                    let leavePlayerList = [];
                    if (value.num_of_turns <= 0) {
                        for (let i = 0; i < value.playerList.length; i++) {
                            let player = value.playerList[i];
                            if (player && !player.ready) {
                                if (now - player.joinTime >= Must_leave_room_time) {
                                    leavePlayerList.push(player);
                                }
                            }
                        }
                        if (leavePlayerList.length > 0) {
                            Logger_1.Logger.log("30秒不准备被踢出==", leavePlayerList);
                            for (let i = 0; i < leavePlayerList.length; i++) {
                                let player = leavePlayerList[i];
                                RoomManager_1.RoomManager.instance.leave_room(value.room_id, player.userid);
                            }
                        }
                    }
                });
            }
        });
    }
    checkDeleteRoom(now) {
        return __awaiter(this, void 0, void 0, function* () {
            if (Config_1.Config.serverType == Config_1.Config.ServerType_List.hall_server) {
                return;
            }
            if (now - this.last_check_delete_room_time >= check_delete_room_time_inteval) {
                this.last_check_delete_room_time = now;
                let deleteRoomList = [];
                let deleteRoomIdList = [];
                let deleteReasonList = [];
                RoomModel_1.RoomModel.roomMap.forEach((room, key) => {
                    let needDelete = false;
                    if (now - room.createTime >= Must_delete_room_time) { //120分钟强制解散房间
                        needDelete = true;
                        deleteReasonList.push("120分钟强制解散房间");
                    }
                    else {
                        if (room.gameInfoBase.applyDismissUserid > 0) { //时间到解散投票截止,自动解散
                            let cd = room.roomConfigBase.dismissTime * 1000;
                            if (now >= (room.gameInfoBase.applyDismissTime + cd)) {
                                needDelete = true;
                                deleteReasonList.push("解散投票时间到自动解散");
                            }
                        }
                    }
                    if (needDelete) {
                        deleteRoomList.push(room);
                        deleteRoomIdList.push(room.roomId);
                    }
                    if (!needDelete) {
                        this.checkRoomGameLogic(room, now);
                        this.checkAutoReady(room, now);
                        // this.checkAutoReleaseZiyou(room, now)
                    }
                });
                if (deleteRoomIdList.length > 0) {
                    RoomManager_1.RoomManager.instance.delete_room(deleteRoomIdList, deleteRoomList, deleteReasonList);
                }
            }
        });
    }
    checkRoomGameLogic(room, now) {
        return __awaiter(this, void 0, void 0, function* () {
            // if (room.game_info instanceof DdzGameInfo) {
            //     ddz_Gamemanager.instance.checkTurnTimeEnd(room, now)
            // }
            // else if (room.game_info instanceof paodekuai_GameInfo) {
            //     paodekuai_Gamemanager.instance.checkTurnTimeEnd(room, now)
            // }
            // else if (room.game_info instanceof jianziding_GameInfo) {
            //     jianziding_Gamemanager.instance.checkTurnTimeEnd(room, now)
            // }
            // else if (room.game_info instanceof huanghua_mj_GameInfo) {
            //     huanghua_mj_Gamemanager.instance.checkTurnTimeEnd(room, now)
            // }
            // else if (room.game_info instanceof cangxian_mj_GameInfo) {
            //     cangxian_mj_Gamemanager.instance.checkTurnTimeEnd(room, now)
            // }
            // else if (room.game_info instanceof dazhonger_mj_GameInfo) {
            //     dazhonger_mj_Gamemanager.instance.checkTurnTimeEnd(room, now)
            // }
        });
    }
    checkAutoReady(room, now) {
        return __awaiter(this, void 0, void 0, function* () {
            if (ServerConfig_1.ServerConfig.nowServerConfig.id == room.serverId) { //游戏服自动准备
                if (room.roomType == RoomConfig_1.RoomConfig.Room_Type.Ziyou) {
                    // if (room.num_of_turns >= 1 && room.num_of_turns <= room.conf["jushu"]) {
                    //     let playerList: Array<RoomPlayer> = room.playerList;
                    //     let isGameStart: boolean = false
                    //     let respModel: ResponseModel = new ResponseModel()
                    //     for (let i = 0; i < playerList.length; i++) {
                    //         let player: RoomPlayer = playerList[i]
                    //         if (player) {
                    //             if (!player.ready) {
                    //                 if (room.lastCheckAutoReadyTIme == 0) {
                    //                     room.lastCheckAutoReadyTIme = now
                    //                     break
                    //                 }
                    //                 if (now - room.lastCheckAutoReadyTIme >= 60 * 1000) { //60秒自动准备
                    //                     if (player.score >= room.conf["minCharge"]) {
                    //                         player.ready = true;
                    //                         if (room.isAllReady()) {
                    //                             isGameStart = true
                    //                         }
                    //                         let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                    //                         if (conn) {
                    //                             respModel.data = room.playerList
                    //                             // conn.createMsgToClient(0, ProtoConfig.notify_update_player, respModel)
                    //                         }
                    //                     }
                    //                 }
                    //             }
                    //         }
                    //     }
                    //     if (isGameStart) {
                    //         RoomManager.instance.notify_game_start(room)
                    //     }
                    // }
                }
            }
        });
    }
    checkAutoReleaseZiyou(room, now) {
        return __awaiter(this, void 0, void 0, function* () {
            if (ServerConfig_1.ServerConfig.nowServerConfig.id == room.server_id) { //游戏服自动准备
                if (room.roomType == RoomConfig_1.RoomConfig.Room_Type.Ziyou) {
                    if (room.num_of_turns >= 1) {
                        let playerList = room.playerList;
                        let isNeedRelease = false;
                        let releaseSeason = "";
                        for (let i = 0; i < playerList.length; i++) {
                            let player = playerList[i];
                            if (player && player.chargeEndTime > 0) {
                                if (now >= player.chargeEndTime && player.score <= room.conf["minCharge"]) { //不够钱够钟就解散
                                    if (player.score >= room.conf["minCharge"]) {
                                        isNeedRelease = true;
                                        releaseSeason = player.nick_name + "金豆不足时间到解散房间";
                                        break;
                                    }
                                }
                            }
                        }
                        if (isNeedRelease) {
                            RoomManager_1.RoomManager.instance.delete_room([room.room_id], [room], [releaseSeason]);
                        }
                    }
                }
            }
        });
    }
}
exports.TimerManager = TimerManager;
TimerManager.instance = new TimerManager();
